<template>
    <div></div>
</template>
<script>
export default {
  created() {
    let that = this;
    that._load();
  },
  methods: {
    _load() {
      let that = this;
      that.$api.ajax([that._getUserInfo()]);
    },
    // 获取用户信息
    _getUserInfo() {
      let that = this;
      return new Promise(function(resolve, reject) {
        that.get_wechat_openId().then(openid => {
          let s_params = {
            openid: openid
          };
          console.log("打印params参数")
          console.log(s_params)
          that.$api
            .getUserInfo(s_params)
            .then(res => {
              that.$storage.set("openid", openid)
              resolve()
                if (res.RCD == 0) {
                  console.log("已有数据");
                  that.$router.push({
                    path: "/Index"
                  });
                } else {
                  console.log("没有数据,去注册");
                  that.$router.push({
                    path: "/Register"
                  })
                }
            })
            .catch(err => {
              resolve()
            })
        });
      });
    },
    // 获取微信openId
    get_wechat_openId(callback) {
      let that = this;
      return new Promise(function(resolve, reject) {
        let code = that.getQueryString("code");
        that.$api.getOpenId(code).then(res => {
          resolve(res.openid);
        });
      });
    },
    // 获取地址参数
    getQueryString(name) {
      try {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return "";
      } catch (e) {
        //TODO handle the exception
      }
    }
  }
};
</script>
